package com.monitor.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.monitor.dao.ErpTirePoliceMapper;
import com.monitor.entity.ErpTirePolice;
import com.monitor.entity.StatisticalAnalysis;
import com.monitor.service.ErpTirePoliceService;
import com.monitor.util.DataUtil;
import com.monitor.util.Result;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 轮胎报警表 服务实现类
 * </p>
 *
 * @author LX
 * @since 2018-09-12
 */
@Service
public class ErpTirePoliceServiceImpl extends ServiceImpl<ErpTirePoliceMapper, ErpTirePolice> implements ErpTirePoliceService {
	@Resource
	private ErpTirePoliceMapper erpTirePoliceMapper;
	
	@Override
	public Result<List<StatisticalAnalysis>> selectTong(String startTime, String endTime, String code, int page, int limit) {
		// TODO Auto-generated method stub
		List<StatisticalAnalysis> list=new ArrayList<>();
		List<Map<String, Object>> tongList=erpTirePoliceMapper.selectTong(startTime, endTime, code, (page-1)*limit, limit);
		List<Integer> cusCodes = new ArrayList<Integer>(); 
		List<String> plates = new ArrayList<String>();
		
		for(Map<String, Object> ma:tongList){
			int index = plates.indexOf(ma.get("plate").toString()+ma.get("tire_location").toString());
			if(index == -1){
				index = cusCodes.indexOf(ma.get("cus_code"));
				if(index == -1){
					StatisticalAnalysis sa = new StatisticalAnalysis();
					sa.setCarNumber(Integer.parseInt(ma.get("cus_vehicletotal").toString()));
					sa.setName(ma.get("cus_name").toString());
					sa.setPlate(ma.get("car_plate").toString());
					sa.setZDplate(ma.get("car_number").toString());
					sa.setZNumber(0);
					sa.setZdate("0");
					sa.setZproportion(0);
					sa.setCNumber(0);
					sa.setCdate("0");
					sa.setCproportion(0);
					sa.setDNumber(0);
					sa.setDdate("0");
					sa.setDproportion(0);
					Long timsL = 0L;
					if(ma.get("solve_dt")!=null) {
						Date solve=DataUtil.strToDateLong(ma.get("solve_dt").toString());
						Date policeDt=DataUtil.strToDateLong(ma.get("police_dt").toString());
						timsL = solve.getTime() - policeDt.getTime();
					}
					
					String timsC = timsL.toString();
					Double policeValue=Double.valueOf(ma.get("police_value").toString());
					Double normalValue=Double.valueOf(ma.get("normal_value").toString());
					if(policeValue > (normalValue*1.05) || (policeValue*1.05) < normalValue){
						sa.setCNumber(1);
						sa.setCdate(timsC);
					}else{
						sa.setDNumber(1);
						sa.setDdate(timsC);
					}
					sa.setGNumber(1);
					sa.setGdate(timsC);
					sa.setGproportion(0);
					list.add(sa);
				}else{
					StatisticalAnalysis sa = list.get(index);
					
					Long timsL = 0L;
					if(ma.get("solve_dt")!=null) {
						Date solve=DataUtil.strToDateLong(ma.get("solve_dt").toString());
						Date policeDt=DataUtil.strToDateLong(ma.get("police_dt").toString());
						timsL = solve.getTime() - policeDt.getTime();
					}
					Double policeValue=Double.valueOf(ma.get("police_value").toString());
					Double normalValue=Double.valueOf(ma.get("normal_value").toString());
					if(policeValue > (normalValue*1.05) || (policeValue*1.05) < normalValue){
						sa.setCNumber(sa.getCNumber()+1);
						sa.setCdate((Integer.decode(sa.getCdate())+ timsL) + "");
					}else{
						sa.setDNumber(sa.getDNumber()+1);
						sa.setDdate((Integer.decode(sa.getDdate())+ timsL) + "");
					}
					sa.setGNumber(sa.getGNumber()+1);
					sa.setGdate((Integer.decode(sa.getGdate())+ timsL) + "");
					list.set(index, sa);
				}
			}
		}
		
		for(int i=0; i< list.size(); i++){
			StatisticalAnalysis sa = list.get(i);
			if(sa.getCarNumber()!=0){
			sa.setZNumber(sa.getCarNumber()-sa.getGNumber());
			sa.setZproportion(sa.getZNumber()*100 / sa.getCarNumber());
			sa.setDproportion(sa.getDNumber()*100 / sa.getCarNumber());
			sa.setCproportion(sa.getCNumber()*100 / sa.getCarNumber());
			sa.setGproportion(sa.getGNumber()*100 / sa.getCarNumber());
			sa.setDdate(longToString(Long.decode(sa.getDdate())));
			sa.setCdate(longToString(Long.decode(sa.getCdate())));
			sa.setGdate(longToString(Long.decode(sa.getGdate())));}
		}
		int count=erpTirePoliceMapper.selectTongCount(startTime, endTime, code);
		return new Result<List<StatisticalAnalysis>>(list, count);
	}

	public String longToString(Long mss){
		//long days = mss / (1000 * 60 * 60 * 24);
		long hours = mss / (1000 * 60 * 60) ;
		long minutes = (mss % (1000 * 60 * 60)) / (1000 * 60);
		long seconds = (mss % (1000 * 60)) / 1000;
		return hours + "小时" + minutes + "分钟" + seconds + "秒"; 
	}
}
